package com.ruoyi.web.product;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.product.domain.vo.CateVo;
import com.ruoyi.product.domain.vo.ProCatVo;
import com.ruoyi.product.service.IProductsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.product.domain.Categories;
import com.ruoyi.product.service.ICategoriesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品类别Controller
 *
 * @author ruoyi
 * @date 2024-12-07
 */
@RestController
@RequestMapping("/product/categories")
public class CategoriesController extends BaseController
{
    @Autowired
    private ICategoriesService categoriesService;

    @Autowired
    private IProductsService productsService;

    /**
     * 查询商品类别列表
     */
    @PreAuthorize("@ss.hasPermi('product:categories:list')")
    @GetMapping("/list")
    public TableDataInfo list(CateVo categories)
    {
        startPage();
        List<CateVo> list = categoriesService.selectCategoriesList(categories);
        return getDataTable(list);
    }
    @GetMapping("/ulist")
    public TableDataInfo ulist(CateVo categories){
        startPage();
        List<CateVo> list = categoriesService.selectUserCategoriesList(categories);
        return getDataTable(list);
    }
    @GetMapping
    public TableDataInfo getCName(){
        List<String> list = categoriesService.selectCName();
        return getDataTable(list);
    }

    /**
     * 导出商品类别列表
     */
//    @PreAuthorize("@ss.hasPermi('product:categories:export')")
//    @Log(title = "商品类别", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Categories categories)
//    {
//        List<Categories> list = categoriesService.selectCategoriesList(categories);
//        ExcelUtil<Categories> util = new ExcelUtil<Categories>(Categories.class);
//        util.exportExcel(response, list, "商品类别数据");
//    }

    /**
     * 获取商品类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:categories:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(categoriesService.selectCategoriesById(id));
    }

    /**
     * 新增商品类别
     */
    @PreAuthorize("@ss.hasPermi('product:categories:add')")
    @Log(title = "商品类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Categories categories)
    {
        return toAjax(categoriesService.insertCategories(categories));
    }

    /**
     * 修改商品类别
     */
    @PreAuthorize("@ss.hasPermi('product:categories:edit')")
    @Log(title = "商品类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Categories categories)
    {
        return toAjax(categoriesService.updateCategories(categories));
    }

    /**
     * 删除商品类别
     */
    @PreAuthorize("@ss.hasPermi('product:categories:remove')")
    @Log(title = "商品类别", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(categoriesService.deleteCategoriesByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('product:catedetail:list')")
    @GetMapping("/detail/list")
    public TableDataInfo detailList(CateVo cateVo){
        startPage();
        System.out.println(cateVo);
        List<ProCatVo> list = productsService.selectCateDetailList(cateVo);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('product:catedetail:list')")
    @GetMapping("/detail/ulist")
    public TableDataInfo udetailList(CateVo cateVo){
        startPage();
        System.out.println(cateVo);
        List<ProCatVo> list = productsService.selectUserCateDetailList(cateVo);
        return getDataTable(list);
    }

}
